﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using NetDon.OA.Interface.Common;

namespace NetDon.OA.Business
{
    public class RouteInfoService : IRouteInfoService
    {
        public bool AddRoute(RouteData route)
        {
            var entity = ConvertToEntityByBusinessData(route, true);
            return entity.Save();
        }

        public bool ModifyRoute(RouteData route)
        {
            var entity = ConvertToEntityByBusinessData(route, false);
            return entity.Save();
        }

        public bool RemoveRoute(Guid routeId)
        {
            var attachmentsCollection = new RouteCollection();
            int result = attachmentsCollection.DeleteMulti(RouteFields.Id == routeId);
            return result > 0;
        }

        public RouteData GetRouteById(Guid routeId)
        {
            var meta = new LinqMetaData();
            var linqResult = (from q in meta.Route
                              where q.Id == routeId
                              select q).FirstOrDefault();

            if (linqResult != null)
            {
                return ConvertToBusinessDataByEntity(linqResult);
            }

            return null;
        }

        public IList<RouteData> GetRoutes()
        {
            var meta = new LinqMetaData();
            var listResult = from q in meta.Route
                             select q;
            List<RouteData> listAttachments = new List<RouteData>();
            if (listResult != null && listResult.Count() > 0)
            {
                foreach (var attachmentsEntity in listResult)
                {
                    var obj = ConvertToBusinessDataByEntity(attachmentsEntity);
                    if (obj != null)
                    {
                        listAttachments.Add(obj);
                    }
                }
            }

            return listAttachments;
        }

        private RouteEntity ConvertToEntityByBusinessData(RouteData routeData, bool isAdd)
        {
            RouteEntity entity = null;
            if (isAdd)
            {
                entity = new RouteEntity();
                entity.Id = routeData.Id;
                if (entity.Id == Guid.Empty)
                {
                    entity.Id = Guid.NewGuid();
                }
            }
            else
            {
                entity = new RouteEntity(routeData.Id);
            }
            entity.RouteName = routeData.RouteName;
            entity.RouteDescription = routeData.RouteDescription;
            entity.TransportationPrefix = routeData.TransportationPrefix;
            entity.AreaId = routeData.AreaId;
            entity.DeliveryOrderCodePrefix = routeData.DeliveryOrderCodePrefix;



            return entity;
        }

        private RouteData ConvertToBusinessDataByEntity(RouteEntity routeEntity)
        {
            return new RouteData
            {
                Id = routeEntity.Id,
                RouteName = routeEntity.RouteName,
                RouteDescription = routeEntity.RouteDescription,
                TransportationPrefix = routeEntity.TransportationPrefix,
                AreaId = routeEntity.AreaId,
                DeliveryOrderCodePrefix = routeEntity.DeliveryOrderCodePrefix
            };
        }


        public bool CheckRouteReferenced(Guid routeId)
        {
            var meta = new LinqMetaData();
            var linqResult = (from q in meta.IssuingOrder
                              where q.Id == routeId
                              select q).Count();

            //todo add other checks
            return linqResult > 0;
        }
    }
}
